/*
a single resource object:
{
  "data": {
    "type": "articles",
    "id": "1",
    "attributes": {
      // ... this article's attributes
    },
    "relationships": {
      // ... this article's relationships
    }
  }
}
resource identifier object:
{
  "data": {
    "type": "articles",
    "id": "1"
  }
}
*/

function rio(tp, origin) {
  return {
    data: {
      type: tp,
      id: origin.id
    }
  };
}

function rios(tp, origins) {
  return origins.map(ori => {
    return {type: tp, id: ori.id};
  });
}

function tpl(tp, origin){
  return {
    data: {
      "type": tp,
      id: origin.id,
      attributes: {}
    }
  };
}

function role(tp, origin) {
  var ja = tpl(tp, origin);
  ja.data.attributes = {name: origin.name};
  return ja;
}

function person(tp, origin) {
  var ja = tpl(tp, origin);
  ja.data.attributes = {
    email: origin.email,
    mobile: origin.mobile,
    avatar: origin.avatar,
    enabled: origin.enabled,
    accountNonExpired: origin.accountNonExpired,
    accountNonLocked: origin.accountNonLocked,
    credentialsNonExpired: origin.credentialsNonExpired,
    emailVerified: origin.emailVerified,
    mobileVerified: origin.mobileVerified,
    gender: origin.gender,
    level: origin.level
  };
  ja.data.relationships = {
    roles: {
      data: rios("role", origin.roles)
    }
  };
  return ja;
}

var convert = {
  role: role,
  person: person
}

export default function hateoasJsonapi(tp, origin) {
  return convert[tp](tp, origin);
}
